package com.dkm.orproject.dkmoa.action.flow;

import com.alibaba.fastjson.JSONObject;
import com.dkm.orproject.dkmoa.domain.OaEntryEducation;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.vo.OaEntryEducationVo;
import com.dkm.orproject.dkmoa.service.flow.OaEntryEducationService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: orproject
 * @DESCRIPTION: 教育经历控制层
 * @USER: 周佳佳
 * @DATE: 2020/8/10 11:49
 */
@RestController
@RequestMapping("/oaEntryEducation")
@Slf4j
public class OaEntryEducationAction {
    @Autowired
    OaEntryEducationService oaEntryEducationService;

    @Resource
    Tools tools;
    /**
     * 根据流程id 查询
     * @param request 请求
     * @param entryId 流程id
     * @return 返回数据
     */
    @UserLoginToken
    @GetMapping("/selectAll")
    public Map<String,Object> findAll(HttpServletRequest request,Long entryId){
        try {
            //分页查询集合
            List<OaEntryEducation> logs=oaEntryEducationService.selectAll(entryId);
            //返回集合
            return Tools.maps(logs,0L);
        } catch (Exception e) {
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogTitle("根据流程id查询教育经历的方法");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 根据流程id 查询(人员信息变更)
     * @param request 请求
     * @param entryId 流程id
     * @return 返回数据
     */
    @UserLoginToken
    @GetMapping("/selectAlls")
    public Map<String,Object> findAlls(HttpServletRequest request,Long entryId){
        try {
            //分页查询集合
            List<OaEntryEducation> logs=oaEntryEducationService.selectAlls(entryId);
            //返回集合
            return Tools.maps(logs,0L);
        } catch (Exception e) {
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogTitle("根据流程id查询教育经历的方法");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }
    /**
     * 教育经历的增加或修改层
     * @param oaEntryEducation 模型对象
     * @param request 请求
     * @return 返回对象
     */
    @UserLoginToken
    @PostMapping("addOrUpdate")
    public Map<String,Object> addOrUpdate(OaEntryEducation oaEntryEducation, HttpServletRequest request){
        OaPersonnel user = (OaPersonnel) request.getSession().getAttribute("user");
        Map<String, Object> map = new HashMap<>(16);
        if(ObjectUtils.isEmpty(user)){
            tools.rowMap(map,0,null);
        }
        try {
            int rows=0;
            OaLog log = new OaLog();
            //如果id不为空 那么就修改 否则增加
            if(oaEntryEducation.getPkEducationId()!=null &&!"".equals(oaEntryEducation.getPkEducationId())){
                //传入更新人
                oaEntryEducation.setUpdatedBy(user.getPersonnelName());
                //传入更新时间
                oaEntryEducation.setUpdatedTime(new Date());
                log.setLogTitle("教育经历修改模块");
                rows = oaEntryEducationService.updateByPrimaryKeySelective(oaEntryEducation);
            }else{
                oaEntryEducation.setCreatedBy(user.getPersonnelName());
                log.setLogTitle("教育经历增加模块");
                oaEntryEducation.setIsRevision("1");
                rows = oaEntryEducationService.insertSelective(oaEntryEducation);
            }
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("教育经历增加或修改模块异常");
            log.setLogType("0");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map, log);
            return map;
        }
    }

    /**
     * 教育经历的增加或修改层(临时)
     * @param oaEntryEducation 模型对象
     * @param request 请求
     * @return 返回对象
     */
    @UserLoginToken
    @PostMapping("addOrUpdates")
    public Map<String,Object> addOrUpdates(OaEntryEducation oaEntryEducation, HttpServletRequest request){
        OaPersonnel user = (OaPersonnel) request.getSession().getAttribute("user");
        Map<String, Object> map = new HashMap<>(16);
        if(ObjectUtils.isEmpty(user)){
            tools.rowMap(map,0,null);
        }
        try {
            int rows;
            OaLog log = new OaLog();
            //如果id不为空 那么就修改 否则增加
            if(oaEntryEducation.getPkEducationId()!=null &&oaEntryEducation.getPkEducationId()>0){
                //传入更新人
                oaEntryEducation.setUpdatedBy(user.getPersonnelName());
                //传入更新时间
                oaEntryEducation.setUpdatedTime(new Date());
                log.setLogTitle("教育经历修改模块");
                rows = oaEntryEducationService.updateByPrimaryKeySelective(oaEntryEducation);
            }else{
                oaEntryEducation.setCreatedBy(user.getPersonnelName());
                log.setLogTitle("教育经历增加模块");
                oaEntryEducation.setIsRevision("2");
                rows = oaEntryEducationService.insertSelective(oaEntryEducation);
            }
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("教育经历增加或修改模块异常");
            log.setLogType("0");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map, log);
            return map;
        }
    }

    /**
     * 删除控制器
     * @param pkEducationId 主键
     * @param session  登录人信息
     * @param request 请求
     * @return 返回数据
     */
    @UserLoginToken
    @DeleteMapping("delete/{pkEducationId}")
    public Map<String, Object> delete(@PathVariable("pkEducationId") Long pkEducationId, HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>(16);
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        try {
            OaLog log = new OaLog();
            OaEntryEducation oaEntryEducation=new OaEntryEducation();
            oaEntryEducation.setPkEducationId(pkEducationId);
            oaEntryEducation.setIsRevision("0");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogTitle("教育经历的删除模块");
            int rows= oaEntryEducationService.updateByPrimaryKeySelective(oaEntryEducation);
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("教育经历的删除模块异常");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            tools.errorMap(map,log);
            return map;
        }
    }


    /**
     * 删除控制器
     * @param pkEducationId 主键
     * @param session  登录人信息
     * @param request 请求
     * @return 返回数据
     */
    @UserLoginToken
    @DeleteMapping("deleteById/{pkEducationId}")
    public Map<String, Object> deleteById(@PathVariable("pkEducationId") Long pkEducationId, HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        try {
            OaLog log = new OaLog();
            OaEntryEducation oaEntryEducation=new OaEntryEducation();
            oaEntryEducation.setPkEducationId(pkEducationId);
            oaEntryEducation.setIsRevision("0");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogTitle("教育经历的删除模块");
            int rows= oaEntryEducationService.updateByPrimaryKeySelective(oaEntryEducation);
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("教育经历的删除模块异常");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 日期格式化 因为前端传的日期是String 后端模型接受的是Date
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }
}
